msm = int(input("Введите четырёхзначное натуральное число: "))
if not 1000 <= msm <= 9999:
    print(0)
else:
    max_prime = 0
    msm_str = str(msm)
    for i in range(4, 0, -1):  
        for j in range(0, 5 - i):
            sub_msm = int(msm_str[j:j+i])
            if sub_msm > 1:
                is_prime = True
                for k in range(2, int(sub_msm**0.5) + 1):
                    if sub_msm % k == 0:
                        is_prime = False
                        break
                if is_prime and sub_msm > max_prime:
                    max_prime = sub_msm
    print(max_prime if max_prime else 0)
